<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Digital;
use App\Models\DigitalAccessNum;
use App\Models\DigitalType;
use App\Models\UserInfo;
use App\Validate\DigitalValidate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 数字资源管理
 */
class DigitalController extends Controller
{
    public $model = null;
    public $typeModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new Digital();
        $this->typeModel = new DigitalType();
        $this->validate = new DigitalValidate();
    }

    /**
     * 数字资源类型
     * @param branch_id int 分馆id  0 获取排除分馆数据   空 表示获取全部数据
     */
    public function typeList()
    {
        // $branch_id = $this->request->input('branch_id', 0);

        $condition[] = ['is_del', '=', 1];
        return $this->typeModel->getFilterList(['id', 'type_name'], $condition);
        // $res = $this->typeModel->getTypeList();
        // if ($res) {
        //     return $this->returnApi(200, '获取成功', true, $res);
        // }
        // return $this->returnApi(203, '暂无数据');
    }


    /**
     * 列表
    // * @param node int 显示位置 1 分馆 2 首页 3 两者同时 多个，逗号拼接
     * @param page int 当前页
     * @param limit int 分页大小
     * @param type_id int 0或空标识全部  类型id
     * @param keywords string 搜索关键词(名称)
     */
    public function lists()
    {
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;
        $keywords = $this->request->keywords;
        $type_id = $this->request->type_id;
        //   $node = $this->request->node;

        $res = $this->model->lists(['id', 'title', 'img', /* 'url', */ 'type_id', 'sort', 'create_time'],  $keywords, $type_id, 1, null, null, 'sort desc,id desc', $limit);

        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);

        return $this->returnApi(200, "获取成功", true, $res);
    }

    /**
     * 数字资源添加浏览量
     * @param id 数字资源id
     */
    public function addBrowseNum()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add_browse_num')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $id = $this->request->id;
        $res = $this->model->where('id', $id)->first();
        if (empty($res)) {
            return $this->returnApi(201, '参数错误');
        }

        DB::beginTransaction();
        try {
            $res->browse_num = $res->browse_num + 1;
            $res->save();

            $digitalAccessNum = new DigitalAccessNum();
            $digitalAccessNum->dayBrowse($id);

            DB::commit();
            return $this->returnApi(200, '操作成功', true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }

    /**
     * 获取数字资源跳转地址
     * @param id int 数字资源id
     */
    public function getDigitalSkipUrl()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('get_digital_skip_url')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $user_id = $this->request->user_info['id'];
        $user_guid = UserInfo::where('id', $user_id)->value('token');
        $url = $this->model->getSkipUrl($this->request->id, $user_guid);
        if ($url) {
            return $this->returnApi(200, '获取成功', true, ['url' => $url]);
        }
        return $this->returnApi(202, '获取失败');
    }
}
